{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1.6 \u5b57\u5178\u4e2d\u7684\u952e\u6620\u5c04\u591a\u4e2a\u503c\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u600e\u6837\u5b9e\u73b0\u4e00\u4e2a\u952e\u5bf9\u5e94\u591a\u4e2a\u503c\u7684\u5b57\u5178\uff08\u4e5f\u53eb multidict\uff09\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u5b57\u5178\u5c31\u662f\u4e00\u4e2a\u952e\u5bf9\u5e94\u4e00\u4e2a\u5355\u503c\u7684\u6620\u5c04\u3002\u5982\u679c\u4f60\u60f3\u8981\u4e00\u4e2a\u952e\u6620\u5c04\u591a\u4e2a\u503c\uff0c\u90a3\u4e48\u4f60\u5c31\u9700\u8981\u5c06\u8fd9\u591a\u4e2a\u503c\u653e\u5230\u53e6\u5916\u7684\u5bb9\u5668\u4e2d\uff0c\n\u6bd4\u5982\u5217\u8868\u6216\u8005\u96c6\u5408\u91cc\u9762\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u6784\u9020\u8fd9\u6837\u7684\u5b57\u5178\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d = {\n    'a' : [1, 2, 3],\n    'b' : [4, 5]\n}\ne = {\n    'a' : {1, 2, 3},\n    'b' : {4, 5}\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9009\u62e9\u4f7f\u7528\u5217\u8868\u8fd8\u662f\u96c6\u5408\u53d6\u51b3\u4e8e\u4f60\u7684\u5b9e\u9645\u9700\u6c42\u3002\u5982\u679c\u4f60\u60f3\u4fdd\u6301\u5143\u7d20\u7684\u63d2\u5165\u987a\u5e8f\u5c31\u5e94\u8be5\u4f7f\u7528\u5217\u8868\uff0c\n\u5982\u679c\u60f3\u53bb\u6389\u91cd\u590d\u5143\u7d20\u5c31\u4f7f\u7528\u96c6\u5408\uff08\u5e76\u4e14\u4e0d\u5173\u5fc3\u5143\u7d20\u7684\u987a\u5e8f\u95ee\u9898\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u4f7f\u7528 collections \u6a21\u5757\u4e2d\u7684 defaultdict \u6765\u6784\u9020\u8fd9\u6837\u7684\u5b57\u5178\u3002\ndefaultdict \u7684\u4e00\u4e2a\u7279\u5f81\u662f\u5b83\u4f1a\u81ea\u52a8\u521d\u59cb\u5316\u6bcf\u4e2a key \u521a\u5f00\u59cb\u5bf9\u5e94\u7684\u503c\uff0c\u6240\u4ee5\u4f60\u53ea\u9700\u8981\u5173\u6ce8\u6dfb\u52a0\u5143\u7d20\u64cd\u4f5c\u4e86\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from collections import defaultdict\n\nd = defaultdict(list)\nd['a'].append(1)\nd['a'].append(2)\nd['b'].append(4)\n\nd = defaultdict(set)\nd['a'].add(1)\nd['a'].add(2)\nd['b'].add(4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c defaultdict \u4f1a\u81ea\u52a8\u4e3a\u5c06\u8981\u8bbf\u95ee\u7684\u952e\uff08\u5c31\u7b97\u76ee\u524d\u5b57\u5178\u4e2d\u5e76\u4e0d\u5b58\u5728\u8fd9\u6837\u7684\u952e\uff09\u521b\u5efa\u6620\u5c04\u5b9e\u4f53\u3002\n\u5982\u679c\u4f60\u5e76\u4e0d\u9700\u8981\u8fd9\u6837\u7684\u7279\u6027\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u666e\u901a\u7684\u5b57\u5178\u4e0a\u4f7f\u7528 setdefault() \u65b9\u6cd5\u6765\u4ee3\u66ff\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d = {} # \u4e00\u4e2a\u666e\u901a\u7684\u5b57\u5178\nd.setdefault('a', []).append(1)\nd.setdefault('a', []).append(2)\nd.setdefault('b', []).append(4)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\u5f88\u591a\u7a0b\u5e8f\u5458\u89c9\u5f97 setdefault() \u7528\u8d77\u6765\u6709\u70b9\u522b\u626d\u3002\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528\u90fd\u5f97\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u521d\u59cb\u503c\u7684\u5b9e\u4f8b\uff08\u4f8b\u5b50\u7a0b\u5e8f\u4e2d\u7684\u7a7a\u5217\u8868 [] \uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0c\u521b\u5efa\u4e00\u4e2a\u591a\u503c\u6620\u5c04\u5b57\u5178\u662f\u5f88\u7b80\u5355\u7684\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u9009\u62e9\u81ea\u5df1\u5b9e\u73b0\u7684\u8bdd\uff0c\u90a3\u4e48\u5bf9\u4e8e\u503c\u7684\u521d\u59cb\u5316\u53ef\u80fd\u4f1a\u6709\u70b9\u9ebb\u70e6\uff0c\n\u4f60\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\u6765\u5b9e\u73b0\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d = {}\nfor key, value in pairs:\n    if key not in d:\n        d[key] = []\n    d[key].append(value)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f7f\u7528 defaultdict \u7684\u8bdd\u4ee3\u7801\u5c31\u66f4\u52a0\u7b80\u6d01\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "d = defaultdict(list)\nfor key, value in pairs:\n    d[key].append(value)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u5c0f\u8282\u6240\u8ba8\u8bba\u7684\u95ee\u9898\u8ddf\u6570\u636e\u5904\u7406\u4e2d\u7684\u8bb0\u5f55\u5f52\u7c7b\u95ee\u9898\u6709\u5927\u7684\u5173\u8054\u3002\u53ef\u4ee5\u53c2\u8003 1.15 \u5c0f\u8282\u7684\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}